﻿using EDMXParserV1;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MVCGenerator
{
    public class DetailsCSHtml
    {
        private SpaceStackStringBuilder sb = new SpaceStackStringBuilder();

        public DetailsCSHtml(String NamespaceName, String PropertyClassName, bool UsingDropDownLists, String PropertyClassPrimaryKeyName, EntitySet SelectedEntitySet, List<GridChoice> GridChoices)
        {
            if (UsingDropDownLists)
            {
                sb.AppendLine("@model " + NamespaceName + ".Models." + PropertyClassName + "Detail");
            }
            else
            {
                sb.AppendLine("@model " + NamespaceName + "." + PropertyClassName + "");
            }

            sb.AppendLine("");
            sb.AppendLine("@{");
            if (true)
            {
                sb.AddSpace();
                sb.AppendLine(@"ViewBag.Title = ""Details"";");
                sb.RemoveSpace();
            }
            sb.AppendLine("}");
            sb.AppendLine("");
            sb.AppendLine("<h2>Details</h2>");
            sb.AppendLine("");
            sb.AppendLine("<fieldset>");
            if (true)
            {
                sb.AddSpace();
                sb.AppendLine("<legend>" + PropertyClassName + "</legend>");
                //foreach (PublicProperty PP in PropertyClass.Properties)
                //{
                foreach (EDMXParserV1.Property Item in SelectedEntitySet.EntityTypeLink.Properties)
                {

                    bool GridChoiceFound = false;
                    GridChoice GC = null;
                    foreach (GridChoice GridChoice in GridChoices)
                    {
                        if (GridChoice.ColumnName == Item.Name)
                        {
                            GridChoiceFound = true;
                            GC = GridChoice;
                            break;
                        }
                    }

                    if (GridChoiceFound)
                    {
                        sb.AppendLine();
                        sb.AppendLine(@"<div class=""display-label"">@Html.LabelFor( x => x." + GC.PrimaryTable + GC.DisplayColumnName + " )</div>");
                        sb.AppendLine(@"<div class=""display-field"">@Model." + GC.PrimaryTable + GC.DisplayColumnName + "</div>");
                    }
                    else
                    {
                        if (Item.IsDateTime())
                        {
                            sb.AppendLine();
                            sb.AppendLine(@"<div class=""display-label"">@Html.LabelFor( x => x." + Item.Name + " )</div>");
                            sb.AppendLine(@"<div class=""display-field"">@String.Format(""{0:g}"", Model." + Item.Name + ")</div>");
                        }
                        else
                        {
                            sb.AppendLine();
                            sb.AppendLine(@"<div class=""display-label"">@Html.LabelFor( x => x." + Item.Name + " )</div>");
                            sb.AppendLine(@"<div class=""display-field"">@Model." + Item.Name + "</div>");
                        }

                    }
                }

                sb.RemoveSpace();
            }
            sb.AppendLine("</fieldset>");

            sb.AppendLine("<p>");
            if (true)
            {
                sb.AddSpace();
                sb.AppendLine(@"@Html.ActionLink(""Edit"", ""Edit"", new { id=Model." + PropertyClassPrimaryKeyName + " }) |");
                sb.AppendLine(@"@Html.ActionLink(""Back to List"", ""Index"")");
                sb.RemoveSpace();
            }
            sb.AppendLine("</p>");
        }

        public override string ToString()
        {
            return sb.ToString();
        }
    }
}
